<template>
  <div class="wrapper">
    <el-card class="form-wrapper" shadow="never">
      <el-form class="form" :model="formData" inline>
        <el-form-item label="电话">
          <el-input v-model="formData.contactPhone" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="商家" prop="merchantId">
          <el-select v-model="formData.merchantId" filterable remote reserve-keyword placeholder="输入搜索"
            :remote-method="getMerchantListApi" style="width: 240px">
            <el-option v-for="item in optionsData.merchants" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="码号">
          <el-input v-model="formData.code" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <!-- <el-form-item label="客户">
          <el-input v-model="formData.username" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="业务员">
          <el-input v-model="formData.nickname" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="客服">
          <el-input v-model="formData.nickname" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="抽奖时间">
          <el-input v-model="formData.nickname" clearable @keyup.enter="resetPage" />
        </el-form-item> -->

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="pager.loading" class="mt-4 !border-none" shadow="never">
      <el-button type="primary" size="small" plain @click="handleAdd">
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        新增
      </el-button>

      <div class="mt-4">
        <el-table :data="pager.lists" size="large">
          <el-table-column label="序号" prop="id" min-width="60" />

          <!-- <el-table-column
            label="客户"
            min-width="100"
            prop="contactName"
          ></el-table-column> -->
          <el-table-column label="客户电话" min-width="100" prop="contact.mobile"></el-table-column>
          <el-table-column label="奖品" min-width="100" prop="prize.name"></el-table-column>
          <el-table-column label="奖品" min-width="100" prop="prizeLevelText"></el-table-column>
          <!-- <el-table-column label="奖品分类" min-width="100" prop="prize.prizeLevel"></el-table-column> -->
          <el-table-column label="活动" min-width="100" prop="activity.title"></el-table-column>
          <el-table-column label="商家" min-width="100" prop="merchant.name"></el-table-column>
          <el-table-column label="活动类型" min-width="100" prop="activityTypeText"></el-table-column>
          <!-- <el-table-column
            label="码号状态"
            min-width="100"
            prop="codeStatus"
          ></el-table-column> -->
          <el-table-column label="订单创建时间" min-width="100" prop="createdTime"></el-table-column>
          <el-table-column label="状态" min-width="100" prop="status">
            <template #default="{ row }">
              <div v-for="(item, index) in optionsData.orderStatus">
                <span v-if="Number(item.value) === Number(row.status)">{{
                  item.name
                  }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button type="primary" link @click="changeStatus(row.id)">
                标记
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    <siginPopup v-if="showSigin" ref="siginRef" @success="getLists" @close="showSigin = false" />
  </div>
</template>

<script lang="ts" setup>
import { deleteApi, listsApi, statusApi } from '@/api/activity/order'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { nextTick, onMounted, reactive, ref, shallowRef } from 'vue'
import pagination from '@/components/pagination/index.vue'
import siginPopup from './sigin.vue'
import { useDictOptions } from '@/hooks/useDictOptions'
import { dictDataLists } from '@/api/setting/dict'
import { listsApi as getMerchantListApi } from '@/api/merchant'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const siginRef = shallowRef<InstanceType<typeof siginPopup>>()
// 表单数据
const formData = reactive({
  contactPhone: '',
  merchantId: '',
  code: '',
})
const showEdit = ref(false)
const showSigin = ref(false)
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: listsApi,
  params: formData,
})
const { optionsData } = useDictOptions<{
  orderStatus: Array<{
    id: number
    name: string
    value: number
    dictTypeId: number
  }>
  merchants: Array<Record<string, any>>
}>({
  orderStatus: {
    api: dictDataLists,
    params: { dictType: 'draw_order_status' },
    transformData(data: any) {
      return data.lists
    },
  }, merchants: {
    api: getMerchantListApi,
    params: {
      name: '',
      pageSize: 10,
    },
    transformData: (data: any) => {
      return data.lists
    },
  },
})
const changeStatus = async (id: number) => {
  showSigin.value = true
  await nextTick()
  siginRef.value?.open()
  siginRef.value?.setFormData({
    id,
  })
}
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await deleteApi({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

onMounted(() => {
  getLists()
})
</script>
<style lang="scss" scoped>
.text_hidden {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.wrapper {
  .form-wrapper {
    padding-bottom: 0px;

    .form {
      // margin-bottom: 14px;
    }
  }
}
</style>
